package com.dkm.ec.activity.domain;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.exception.PartinException;
import com.dkm.ec.activity.persistence.dao.SecKillActivityConfigQueryDao;
import com.dkm.ec.activity.persistence.dao.SecKillActivityPartinQueryDao;
import com.dkm.ec.activity.persistence.dao.SecSkillActivitySkuDao;
import com.dkm.ec.activity.persistence.dao.SecSkillActivitySkuQueryDao;
import com.dkm.ec.activity.persistence.entity.SecKillActivityConfigPo;
import com.dkm.ec.activity.persistence.entity.SecKillActivityPartinPo;
import com.dkm.ec.activity.persistence.entity.SecSkillActivitySkuPo;
import com.dkm.ec.so.persistence.entity.SoItemPo;
import com.dkm.ec.stock.persistence.dao.StockDao;
import com.dkm.ec.stock.persistence.dao.StockQueryDao;
import com.dkm.ec.stock.persistence.entity.StockPo;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author hhq
 * @create 2017/8/14
 */
@SuppressWarnings("serial")
@Service
@Scope("prototype")
public class SecSkillActivitySku extends AbstractDomain<String, SecSkillActivitySkuPo> {

    private SecSkillActivitySkuDao secSkillActivitySkuDao = null;

    @Resource
    private SecSkillActivitySkuQueryDao secSkillActivitySkuQueryDao;
    @Resource
    private SecKillActivityPartinQueryDao secKillActivityPartinQueryDao;
    @Resource
    private SecKillActivityConfigQueryDao secKillActivityConfigQueryDao;
    @Resource
    private StockDao stockDao;
    @Resource
    private StockQueryDao stockQueryDao;

    @Override
    protected void init() {
        secSkillActivitySkuDao = SpringHelper.getBean(SecSkillActivitySkuDao.class);
        setDao(secSkillActivitySkuDao);
    }

    /**
     * 扣除商品
     * @param soItemPo
     */
    public void deduct(SoItemPo soItemPo) {
        if(!soItemPo.getActivityType().equals("1")) {
            throw new PartinException(String.format("id：%s 活动类型有误", soItemPo.getId()));
        }
        SecSkillActivitySkuPo skuPo = secSkillActivitySkuQueryDao.get(soItemPo.getActSkuId() + "");
        SecKillActivityPartinPo partinPo = secKillActivityPartinQueryDao.get(skuPo.getPartinid());
        SecKillActivityConfigPo configPo = secKillActivityConfigQueryDao.get(partinPo.getActivityid());
        Date currentTime = new Date();
        if(currentTime.after(configPo.getBeginTime()) && currentTime.before(configPo.getEndTime())) {
            if(soItemPo.getCount() > skuPo.getAvailableNum()) {
                throw new PartinException(String.format("id：%s 商品不足", soItemPo.getId()));
            }
            skuPo.setAvailableNum(skuPo.getAvailableNum() - soItemPo.getCount());
            secSkillActivitySkuDao.update(skuPo);
        }else {
            throw new PartinException(String.format("id：%s 不在活动时间范围内", soItemPo.getId()));
        }
    }

    /**
     * 回退商品
     * @param soItemPo
     */
    public void sendBack(SoItemPo soItemPo) {
        if(!soItemPo.getActivityType().equals("1")) {
            throw new PartinException(String.format("id：%s 活动类型有误", soItemPo.getId()));
        }
        SecSkillActivitySkuPo skuPo = secSkillActivitySkuQueryDao.get(soItemPo.getActSkuId() + "");
        // 判断是否归还总库存
        if("0".equals(skuPo.getIsReturnStock())) { // 0: 没有归还总库存
            skuPo.setAvailableNum(skuPo.getAvailableNum() + soItemPo.getCount());
            secSkillActivitySkuDao.update(skuPo);
        }else { // 1: 已经归还总库存
            StockPo stockPo = stockQueryDao.get(skuPo.getStockid());
            stockPo.setQty(stockPo.getQty() + soItemPo.getCount());
            stockDao.update(stockPo);
        }
    }

}
